package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.pojo.entity.PublishCheckLog;
import com.itheima.dubbo.interfaces.PublishCheckLogApi;
import com.itheima.dubbo.mysql.mapper.PublishCheckLogMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/20
 * @Version 1.0
 * @describe
 */
@Service
public class PublishCheckLogApiImpl implements PublishCheckLogApi {

    @Autowired
    private PublishCheckLogMapper publishCheckLogMapper;

    /**
     * 记录审核日志
     *
     * @param publishCheckLog
     */
    @Override
    public void savePublishCheckLog(PublishCheckLog publishCheckLog) {
        publishCheckLogMapper.insert(publishCheckLog);
    }

    /**
     * 分页查询审核日志记录
     *
     * @param page      当前页
     * @param pagesize  每页条数
     * @param username  操作人
     * @param operation 操作内容
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @Override
    public List<PublishCheckLog> findCheckLog(Integer page, Integer pagesize, String username, String operation, String startTime, String endTime) {
        Page<PublishCheckLog> publishCheckLogPage = publishCheckLogMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(PublishCheckLog.class)
                        .eq(StrUtil.isNotBlank(username), PublishCheckLog::getAdmin, username)
                        .like(StrUtil.isNotBlank(operation), PublishCheckLog::getOperation, operation)
                        .gt(StrUtil.isNotBlank(startTime), PublishCheckLog::getCreated, Convert.toDate(startTime))
                        .lt(StrUtil.isNotBlank(endTime), PublishCheckLog::getCreated, Convert.toDate(endTime))
                        .orderByDesc(PublishCheckLog::getCreated));
        return publishCheckLogPage.getRecords();
    }
}
